<?php

/**
 * application actions.
 *
 * @package    wills
 * @subpackage application
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class ApplicationActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {
	$this->area = Doctrine::getTable('Area')->findOneBySlug($request->getParameter('area'));

    $this->applications = Doctrine::getTable('Application')
      ->createQuery('a')
      ->execute();
  }

  public function executeLicense(sfWebRequest $request)
  {
	 $this->areas = Doctrine::getTable('Area')->getHomepageList();     
  }

  public function executeNew(sfWebRequest $request)
  {
	 $this->areas = Doctrine::getTable('Area')->getHomepageList();
	 $this->form = new ApplicationForm(); 
  }

  public function executeCreate(sfWebRequest $request)
  {
	  $this->areas = Doctrine::getTable('Area')->getHomepageList();
    $this->forward404Unless($request->isMethod(sfRequest::POST));

    //以下几行为之前根据当前赛区存储赛区，现已改为根据用户自己选择赛区
	//$application = new Application();
    //$area = Doctrine::getTable('area')->findOneBySlug($request->getParameter('area'));
    //$application->setArea($area);
    
    // $this->form = new ApplicationForm($application);
	//$this->area = Doctrine::getTable('Area')->findOneBySlug($request->getParameter('area'));//获得赛区
    $this->form = new ApplicationForm(); 

    $this->processForm($request, $this->form);

    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {		
	 $this->areas = Doctrine::getTable('Area')->getHomepageList();     //获得赛区
	    
	$userid=sfContext::getInstance()->getUser()->getAttribute('id');//登录会员id
	$contestant =Doctrine_Query::create()
				 ->from('Contestant')
				 ->Where('application_id = ?', $userid)
				 ->fetchOne();
	if($contestant)
	  {
		$cid=$contestant->getId();//登录会员id转换为选手id，以获取图片用到的contestant_id
		$this->cid=$cid;//模板用到的选手cid-cid
	  }
  	$this->application = Doctrine::getTable('Application')->findOneById($userid);//获得整条记录
	$this->ispassed =$this->application->getIsPassed(); //是否通过选拔成为选手
    $this->forward404Unless($application = Doctrine::getTable('Application')->find(array($userid)), sprintf('Object application does not exist (%s).', $userid));

    $this->form = new ApplicationForm2($application);
  }

  public function executeUpdate(sfWebRequest $request)
  {
	
	$this->areas = Doctrine::getTable('Area')->getHomepageList();//获得赛区
	 //获得当前用户
 	$userid=sfContext::getInstance()->getUser()->getAttribute('id');//登录会员id
	$contestant =Doctrine_Query::create()
				 ->from('Contestant')
				 ->Where('application_id = ?', $userid)
				 ->fetchOne();
	if($contestant)
	  {
		$cid=$contestant->getId();//登录会员id转换为选手id，以获取图片用到的contestant_id
		$this->cid=$cid;//模板用到的选手cid-cid
	  }
	$this->application = Doctrine::getTable('Application')->findOneById($userid);//获得整条记录
	$this->ispassed =$this->application->getIsPassed(); //是否通过选拔成为选手
    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
    $this->forward404Unless($application = Doctrine::getTable('Application')->find(array($userid)), sprintf('Object application does not exist (%s).', $userid));
   // $this->form = new ApplicationForm($application);

	$this->form = new ApplicationForm2($this->getRoute()->getObject());
	$this->process2Form($request, $this->form);
	
	$this->setTemplate('edit');

  }

  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    $this->forward404Unless($application = Doctrine::getTable('Application')->find(array($request->getParameter('id'))), sprintf('Object application does not exist (%s).', $request->getParameter('id')));
    $application->delete();

    $this->redirect('application/index');
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $inputs = $request->getParameter($form->getName());
	foreach ($inputs as &$input)
    {
      $input = trim($input);
    }

    $form->bind($inputs, $request->getFiles($form->getName()));
	
    if ($form->isValid())
    {
      $application = $form->save();
	  $this->getUser()->setFlash('message', '恭喜你,注册成功。');
	  $this->getUser()->setAttribute('id', $application->getId());
      $this->getUser()->setAttribute('account', $application->getName());
      $this->getUser()->setAuthenticated(true);
		$areaid=$application->getAreaId();
		$herarea =Doctrine_Query::create()
				 ->from('Area')
				 ->Where('id = ?', $areaid)
				 ->fetchOne();
		$areaslug =$herarea->getSlug();
     $this->redirect('@application_edit?id='. $application->getId().'&area='.$areaslug);//跳转至个人中心编辑页面
	    }
  }

	  protected function process2Form(sfWebRequest $request, sfForm $form)
  {
    $inputs = $request->getParameter($form->getName());
	foreach ($inputs as &$input)
    {
      $input = trim($input);
    }

    $form->bind($inputs, $request->getFiles($form->getName()));
	
    if ($form->isValid())
    {
      $application = $form->save();
	  $this->getUser()->setFlash('message', '恭喜你，修改成功。');
	  $this->getUser()->setAttribute('id', $application->getId());
      $this->getUser()->setAttribute('account', $application->getName());
      $this->getUser()->setAuthenticated(true);

     $this->redirect('@application_edit?id='. $application->getId());//跳转至个人中心编辑页面
	    }
  }



}
